package com.etlmaster.executor.boot;

import java.io.IOException;
import java.util.Map;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.stereotype.Component;

import com.etlmaster.executor.EtlMasterExecutor;
import com.etlmaster.executor.Job;
import com.etlmaster.executor.JobPool;
import com.etlmaster.executor.parser.XmlJobDbLoader;
import com.etlmaster.executor.parser.XmlJobFileLoader;
import com.etlmaster.executor.parser.XmlJobParser;
import com.etlmaster.executor.utils.DpExecutorUtils;
import com.etlmaster.executor.utils.LoadDataSourceInfo;
import com.etlmaster.executor.utils.LogWriter;

/**
 * 流程的执行器引导程序
 */
@Component
public class EtlMasterExecutorBoot {
	private final static String 
		JOB_ID = "f",
		XML_FILE = "p";
	private Job job;
	private String session_id="";
	/**
	 * 判断外部参数是xml文件还是procName
	 * @param args
	 * @return
	 * @throws IOException
	 */
	public boolean execute(String paramStr) throws IOException{

		LogWriter.addLog("INFO", "外部参数：" + paramStr);
		Map<String,Object> params = DpExecutorUtils.parseArgs(paramStr);
		XmlJobParser parser;
		if(params.containsKey(JOB_ID)){
			String jobId = (String)params.get(JOB_ID);
			parser=new XmlJobDbLoader();
			job =parser.builder(jobId,params);
		}else if(params.containsKey(XML_FILE)){
			String fileName = (String)params.get(XML_FILE);
			parser=new XmlJobFileLoader();
			job = parser.builder(fileName,params);
		}
		session_id=job.getSeesionId();
		if(job!=null){
			EtlMasterExecutor dpExecutor = new EtlMasterExecutor();
			Boolean flag=dpExecutor.executeByStack(job);
			JobPool.destoryVariable(job.getSeesionId());
			return flag;
		}else{
			return false;
		}
	}
	
	public static void main(String[] args) {
		EtlMasterExecutorBoot boot =null;
		try {
			
			StringBuilder sb = new StringBuilder();
			
			if(args.length==0){
				System.out.printf("Usage:						   [-f procName\n");
				System.out.printf("                                [-teamCode 租户所属组，数据操作权限使用\n");
				System.out.printf("                                [-t dateTime]\n");
				System.out.printf("                                [-i 步骤号(0 默认运行，大于0自动到断点步骤运行)]\n");
				System.out.printf("                                [-ignore true忽略写数据库步骤运行日志，常驻进程使用]\n");
				System.out.printf("                                [-logFile 输出日志名称附加，并发进程使用]\n");
				System.out.printf("                                [-isDebug 是否输出类名，方法名]\n");
				System.out.printf("                                [-e 步骤号，程序停止步骤\n");
			}
			for (String arg : args) {
				sb.append(arg).append(" ");
			}
			sb.deleteCharAt(sb.length()-1);
			
			ApplicationContext context = new FileSystemXmlApplicationContext(new String[]{"classpath:conf/spring/*.xml","conf/spring/*.xml","conf/*.xml"});
			boot= context.getBean(EtlMasterExecutorBoot.class);
			
			LoadDataSourceInfo.loadDataInfo(sb.toString());
			if(!boot.execute(sb.toString())){
				EtlMasterExecutor.error(boot.job);
			}else{
				LogWriter.addLog("INFO","程序执行成功！");
				System.exit(0);
			}
		} catch (Throwable ex) {
			LogWriter.addLog("ERROR","异常信息：{}",ex.getMessage());
			ex.printStackTrace();
			EtlMasterExecutor.error(boot.job);
		}
	}

	public String getSession_id() {
		return session_id;
	}

	public void setSession_id(String session_id) {
		this.session_id = session_id;
	}
	
}
